import request from "@/utils/request";

import type {
  GetUserListApiParams,
  GetUserListApiResponse,
  AddUserApiParams,
  UpdateApiParams,
  GetUserRoleListApiResponse,
} from "./model/userModel";

const enum Api {
  getUserList = "/admin/acl/user",
  addUser = "/admin/acl/user/save",
  updateUser = "/admin/acl/user/update",
  removeUser = "/admin/acl/user/remove",
  getUserRoleList = "/admin/acl/user/toAssign",
  assignUserRoleList = "/admin/acl/user/doAssign",
}

/**
 * 获取用户列表
 * @returns GetUserListApiResponse
 */
export const getUserListApi = ({
  page,
  limit,
  username,
}: GetUserListApiParams) => {
  return request.get<any, GetUserListApiResponse>(
    Api.getUserList + `/${page}/${limit}`,
    {
      params: {
        username,
      },
    }
  );
};

// 新增用户
export const addUserApi = (data: AddUserApiParams) => {
  return request.post<any, null>(Api.addUser, data);
};

// 修改用户
export const updateUserApi = (data: UpdateApiParams) => {
  return request.put<any, null>(Api.updateUser, data);
};

// 删除用户
export const removeUserApi = (id: string) => {
  return request.delete<any, null>(Api.removeUser + `/${id}`);
};

// 获取角色列表
export const getUserRoleListApi = (userId: string) => {
  return request.get<any, GetUserRoleListApiResponse>(
    Api.getUserRoleList + `/${userId}`
  );
};

// 分配用户角色列表
export const assignUserRoleListApi = (userId: string, roleId: string[]) => {
  return request.post<any, null>(
    Api.assignUserRoleList,
    {},
    {
      params: {
        userId,
        roleId: roleId.join(","),
      },
    }
  );
};
